package biolabs.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.io.OutputStream;
import java.util.logging.Handler;

import javax.swing.Box;
import javax.swing.JList;
import javax.swing.JSplitPane;

import biolabs.core.Application;
import biolabs.core.ApplicationListener;
import biolabs.core.Experiment;


/**
 * Stage represents the BioLabs' GUI.
 * It can be used in a desktop application or in an applet.
 * It is the container for other UI components.
 *
 * @author Tomas Mikula
 */
public class Stage implements UIComponent, ApplicationListener {

	/**
	 * Name of the logger used by GUI components.
	 */
	public static final String LOGGER = "biolabs.gui";

	/**
	 * Dimension representing unlimited width and height.
	 */
	public static final Dimension UNLIMITED_SIZE =
		new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);

	private final Component content;

	private final ExperimentList eList;
	private final ExperimentDetail eDetail;
	private final ExperimentExplorer eExpl;
	private final ConsoleComponent console;
	private final InputComponent input;

	public Stage(Application app) {

		/* create visual components */
		eList = new ExperimentList();
		eDetail = new ExperimentDetail(app);
		eExpl = new ExperimentExplorer();
		JList nodeProps = new JList();
		console = new ConsoleComponent(app);
		input = new InputComponent(console);

		/* set relationships */
		eList.addSelectionListener(eDetail);
		eList.addSelectionListener(eExpl);


		/* lay out */

		eExpl.getContent().setMaximumSize(UNLIMITED_SIZE);

		Box left = Box.createVerticalBox();
		left.add(eList.getContent());
		left.add(eDetail.getContent());
		left.add(eExpl.getContent());
		left.add(nodeProps);

		JSplitPane right = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
				true, console.getContent(), input.getContent());

		this.content = new JSplitPane(
				JSplitPane.HORIZONTAL_SPLIT, true, left, right);

		/* listen to app's events */
		app.addListener(this);
	}

	@Override
	public Component getContent() {
		return content;
	}

	public Handler getLogHandler() {
		return console.getLogHandler();
	}

	public OutputStream getOutputStream() {
		return console.getOutputStream();
	}

	public OutputStream getErrorStream() {
		return console.getErrorStream();
	}

	@Override
	public void experimentCreated(Experiment exp) {
		eList.experimentCreated(exp);
	}
}
